<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="com.engine.workflow.biz.requestForm.FileBiz" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.jobs.common.DocUtil" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    JSONObject jsonData = new JSONObject();
     BaseBean baseBean = new BaseBean();
     String URL = baseBean.getPropValue("zhlxpdm", "url");
    String workflowid = Util.null2String(request.getParameter("workflowid"));
    String itemNumber = Util.null2String(request.getParameter("itemNumber"));
    User user = HrmUserVarify.getUser(request, response);
    try {
       String url = URL+"/ExternalData/getPdf?itemNumber="+itemNumber;
        Map<String, Object> retMap = DocUtil.downloadByUrl(url);
        if (Util.null2String(retMap.get("code")).equals("0")) {
            String filepath = Util.null2String(retMap.get("data"));
            int imagefileid = DocUtil.saveImageFile(itemNumber + ".pdf", filepath);
            int seccategory =getDoccategory(workflowid);
            int docid = DocUtil.createDocByImagefileId(imagefileid, user, "1", "0", itemNumber + ".pdf", "", seccategory);
            if (docid > 0) {
                Object resultObj = getFileDataList(docid+"",1,user);
                jsonData.put("filedatas",resultObj);
                jsonData.put("docid", docid);
                jsonData.put("status", "success");
            } else {
                jsonData.put("status", "fail");
            }
        }else {
            jsonData.put("status", "fail");
        }
    } catch (Exception e) {
        jsonData.put("status", "exception");
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>
<%!

    private static BaseBean baseBean = new BaseBean();
    /**
     * 获取附件上传目录ID
     */
    public static int getDoccategory(String workflowid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select replace(doccategory,',','') from workflow_base where id=?", workflowid);
        rs.next();
        return Util.getIntValue(rs.getString(1), -1);
    }

    //返回list
    public static Object getFileDataList(String docIds, int type, User user) {
        baseBean.writeLog(">>>>>getFileDataList>>>>docIds=" + docIds);
        return FileBiz.getFileFieldSpecialObj(docIds, type, new HashMap<>(), user);
    }
%>
